layui.use('form', function() {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery;
    var indexLoa;
    var base = new Base64();
    var math = Math.random().toString(36).substr(2);
    $.ajaxSetup({ cache: false })
    //获取验证码
    loginCode();
    function loginCode() {
        math = base.encode(math)
        $.ajax({
            url: "gt/opm/login/captcha", //请求地址
            type: "GET", //提交的方式S
            data: { "uuid": math }, //提交的数据
            dataType: "JSON", //返回类型 TEXT字符串 JSON XML
            headers: { "Content-Type": "application/json;charset=UTF-8" },
            beforeSend: function() {
                indexLoa = layer.load(2, {
                    shade: [0.1, '#000']
                })
            },
            success: function(data) {
                layer.close(indexLoa);
                if (data.flag == true) {
                    $(".codeBGImg").attr("src", data.data);
                    $(".codeBGImg").attr("uuid", math);
                } else {
                    layer.alert(data.msg, { icon: 2, time: 2000 });
                }
            }
        })
    }
   
    // 事件
    var active = {
            // 更新验证码
            codeB: function() {
                loginCode()
            }
        }
        
        //监听提交
    form.on('submit(login)', function(data) {
        var jobres=data;
        jobres.elem.disabled=true;
        var valIp = data.field
        var Iname= valIp.username.replace(/(^\s*)|(\s*$)/g, "");
        var Ipassword= valIp.password.replace(/(^\s*)|(\s*$)/g, "");
        var oldCode = $(".codeBGImg").attr("uuid");
        math = Math.random().toString(36).substr(2);
        var inputCode = valIp.code;
        // 验证验证码
        $.ajax({
            url: "gt/opm/login/captcha/check", //请求地址
            type: "get", //提交的方式S
            data:{"uuid":oldCode,"code":inputCode},
            dataType: "JSON", //返回类型 TEXT字符串 JSON XML
            headers: { "Content-Type": "application/json;charset=UTF-8" },
            beforeSend: function() {
                indexLoa = layer.load(2, {
                    shade: [0.1, '#000']
                })
            },
            success: function(data) {
                layer.close(indexLoa);
                if (data.flag == true){
                   
                    // 获取token
                    $.ajax({
                        url: "gt/opm/login/token", //请求地址
                        type: "get", //提交的方式S
                        data:{"loginName":Iname},
                        dataType: "JSON", //返回类型 TEXT字符串 JSON XML
                        headers: { "Content-Type": "application/json;charset=UTF-8" },
                        beforeSend: function() {
                            indexLoa = layer.load(2, {
                                shade: [0.1, '#000']
                            })
                        },
                        success: function(data) {
                            var str = {
                                'loginPort': '12345',
                                'loginName':Iname,
                                'loginPwd': Ipassword
                            }
                            var token=data.data;
                            if(data.flag){
                                sessionStorage.setItem("accessToken",token);
                                // 验证用户
                                $.ajax({
                                    url: "gt/opm/login/check", //请求地址
                                    type: "get", //提交的方式S
                                    data:str,
                                    dataType: "JSON", //返回类型 TEXT字符串 JSON XML
                                    headers: { "Content-Type": "application/json;charset=UTF-8" },
                                    beforeSend: function(request) {
                                        request.setRequestHeader("access-token",token)
                                        indexLoa = layer.load(2, {
                                            shade: [0.1, '#000']
                                            
                                        })
                                       
                                    },
                                    success: function(data) {
                                        layer.close(indexLoa);
                                        if (data.flag == true) {
                                            sessionStorage.setItem("userDept", data.data.userDept);
                                            sessionStorage.setItem("userid", data.data.id);
                                            sessionStorage.setItem("loginName", data.data.loginName);
                                            location.href = './index.html'
                                        } else {
                                            loginCode()
                                            jobres.elem.disabled=false
                                            layer.alert(data.msg, { icon: 2, time: 2000 });
                                        }
            
                                    }, error: function(data) {
                                        loginCode()
                                        jobres.elem.disabled=false
                                    }
                                })
                            }
                        }
                    })
                   
                }else{

                    loginCode()
                    jobres.elem.disabled=false
                    layer.msg(data.msg, { icon: 2, time: 2000 });
                }
            }
        })
        return false;
    });
    //回车事件绑定
    $(document).keydown(function (e) {
        if (e.keyCode === 13) {
        $("#submitKey").trigger("click");
        }
    });
    // 监听点击事件
    $('.bao_loginbtn').on('click', function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

});